// Register page module
// ====================

define(['zepto', 'chart', 'wx', 'sui', 'modernizrCustom', 'pagetransitions'], function($, Chart, wx) {
  return {
    init: function(data) {
      this.physicalAttrScores = parseInt(data.physicalAttrScores);
      this.sportsKnowledgeScores = parseInt(data.sportsKnowledgeScores);
      this.exerciseRegularlyScores = parseInt(data.exerciseRegularlyScores);
      this.physicalMentalPleasureScores = parseInt(data.physicalMentalPleasureScores);
      this.sportsPracticeSkillScores = parseInt(data.sportsPracticeSkillScores);
      this.inviter = data.inviter;
      this.course = data.course;
      
      this.initChart();
      this.bindClick();
      var shareData = this.initShareData();
      var isDebug = location.search.indexOf('debug=true') > 0
      wx.config({
        debug: isDebug,
        appId: data.jsConfig.appid,
        timestamp: data.jsConfig.timestamp,
        nonceStr: data.jsConfig.nonceStr,
        signature: data.jsConfig.signature,
        jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage']
      })
      wx.ready(function() {
        wx.onMenuShareTimeline({
          title: shareData.name,
          desc:  shareData.desc,
          link:  shareData.link,
          imgUrl: shareData.cover
        })
        wx.onMenuShareAppMessage({
          title: shareData.name,
          desc:  shareData.desc,
          link:  shareData.link,
          imgUrl: shareData.cover
        })
      })
      
    },
    
    initChart: function() {
      var that = this;
      var ctx = document.getElementById("ftChart").getContext("2d");
      var data = {
          labels : ["执行力","知识储备","运动心理","实践技能","身体属性"],
          datasets : [
            {
              fillColor : "rgba(193, 193, 193, 0.58)", // 背景色
              strokeColor : "rgba(193, 193, 193, 0.58)", // 线
              pointColor : "#bdab49", // 点
              pointStrokeColor : "#bdab49", 
              data : [that.physicalAttrScores,that.sportsKnowledgeScores,that.exerciseRegularlyScores,that.physicalMentalPleasureScores,that.sportsPracticeSkillScores]
            }
          ]
        }
      
      var options = {
          scaleLineColor : "#fff",
          scaleFontColor : "#fff",
          angleLineColor : "#fff",
          pointLabelFontColor : "#fff",
          pointDot: false
        }
      
      new Chart(ctx).Radar(data, options);
    },
    
    bindClick: function() {
      $(document).on("click", ".result-share", function(e) {
        e.preventDefault();
         var modalHtml = '<div class="modal-overlay share-modal">' +
                           '<div class="modal-descrption"><p>邀请1位好友完成测试，<br/>一起运动打卡咯～</p>'+
                           '<img src="../resource/image/share-hint.png" width="100%">'+
                         '</div></div>'
         $("body").append(modalHtml)
      })
     
       $(document).on("click", ".share-modal", function(e) {
           e.preventDefault();
           $(this).remove();
       })
    },
    
    initShareData: function() {
      var that = this;
      return {
        name:  "我的运动测试等级是"+ that.course + "，和我一起来人马君运动打卡吧~",
        desc:  "［人马君运动打卡计划］，专业减脂打卡课程，社群主教在线答疑指导，简洁易懂养成坚持运动好习惯。",
        link:   window.__CTX__.domain + "/questionnaires/result-share?inviter=" + that.inviter,
        cover:  window.__CTX__.domain + "/resource/image/movement-clock.jpg"
      }
    }
  }
})